import pygame

class Scaleplate:
    """
    标尺
    """
    def __init__(self, car) -> None:
        self.car = car
        self.screen = car.screen
        pass

    def update(self):
        fg_color = (100, 100, 100)
        bg_color = (255, 255, 255)
        font = pygame.font.SysFont(None, 20)
        screen_width, screen_height = self.screen.get_rect().size
        # 画横标尺
        self.draw_scaleplate_h(fg_color, bg_color, font, screen_width)
        # 画纵标尺
        self.draw_scaleplate_v(fg_color, bg_color, font, screen_height)
        # 画左上角
        pygame.draw.rect(self.screen, bg_color, (0, 0, 25, 25))
        pygame.draw.line(self.screen, fg_color, (0, 25), (25, 25))
        pygame.draw.line(self.screen, fg_color, (25, 0), (25, 25))
        pass

    def draw_scaleplate_h(self, fg_color, bg_color, font, win_width):
        # 画标尺栏
        pygame.draw.rect(self.screen, bg_color, (0, 0, win_width, 25))
        pygame.draw.line(self.screen, fg_color, (0, 25), (win_width, 25))
        # 画刻度
        for win_x in range(win_width):
            world_x = self.car.winx_to_worldx(win_x)
            if world_x % 100 == 0:
                lb_image = font.render(f"{world_x}", True, fg_color, bg_color)
                lb_rect = lb_image.get_rect()
                lb_rect.x = win_x + 5
                lb_rect.y = 10
                self.screen.blit(lb_image, lb_rect)
                pygame.draw.line(
                    self.screen,
                    fg_color,
                    (win_x, 0),
                    (win_x, 20),
                )
            elif world_x % 10 == 0:
                pygame.draw.line(
                    self.screen,
                    fg_color,
                    (win_x, 0),
                    (win_x, 10),
                )

    def draw_scaleplate_v(self, fg_color, bg_color, font, win_height):
        # 标尺栏
        pygame.draw.rect(self.screen, bg_color, (0, 0, 25, win_height))
        pygame.draw.line(self.screen, fg_color, (25, 0), (25, win_height))
        # 画刻度
        for win_y in range(win_height):
            world_y = self.car.winy_to_worldy(-win_y)
            if world_y % 100 == 0:
                lb_image = font.render(f"{world_y}", True, fg_color, bg_color)
                lb_image = pygame.transform.rotate(lb_image, -90)
                lb_rect = lb_image.get_rect()
                lb_rect.x = 10
                lb_rect.y = win_y + 5
                self.screen.blit(lb_image, lb_rect)

                pygame.draw.line(
                    self.screen,
                    fg_color,
                    (0, win_y),
                    (20, win_y),
                )
            elif world_y % 10 == 0:
                pygame.draw.line(
                    self.screen,
                    fg_color,
                    (0, win_y),
                    (10, win_y),
                )
            pass